package com.cskaoyan.market.admin.controller;

import com.cskaoyan.market.admin.service.MarketCategoryService;
import com.cskaoyan.market.db.domain.MarketCategory;
import com.cskaoyan.market.db.vo.MarketCategoryL1Vo;
import com.cskaoyan.market.db.vo.MarketCategoryVo;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

import static com.cskaoyan.market.util.GetReqBodyUtil.getRequestBody;

/**
 * @NAME: AdminCategoryController
 * @DESCRIPTION: 商品类目controller层
 * @DATE: 2023/5/7 15:32
 * @AUTHOR: 刘超
 */
@SuppressWarnings("DuplicatedCode")
@RestController
@RequestMapping("/admin/category/*")
@MultipartConfig
public class AdminCategoryController{
    @Autowired
    MarketCategoryService marketCategoryService;

    /**
     * 获得父级目录
     * @param req
     * @param resp
     */
    private void l1(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        List<MarketCategoryL1Vo> marketCategoryL1Vos = marketCategoryService.l1();
        resp.getWriter().println(JacksonUtil.
                getObjectMapper().writeValueAsString(ResponseUtil.okList(marketCategoryL1Vos)));
    }


    /**
     * 查询商品类别
     *
     * @param req
     * @param resp
     */
    @RequestMapping("/list")
    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {


        List<MarketCategoryVo> marketCategoryVos = marketCategoryService.list();

        resp.getWriter().println(JacksonUtil.
                getObjectMapper().writeValueAsString(ResponseUtil.okList(marketCategoryVos)));


    }

    /**
     * 新建商品类别
     *
     * @param req
     * @param resp
     */
    @RequestMapping("/create")
    private void create(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("create");
        // 读入请求参数
        String requestBody = getRequestBody(req);
        MarketCategory marketCategory = JacksonUtil.getObjectMapper().readValue(requestBody, MarketCategory.class);

        if (StringUtils.isEmpty(marketCategory.getName())) {
            resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.badArgument()));
            return;
        }
        // 调用service层接口
        marketCategory.setSortOrder((byte) 1);
        marketCategory.setAddTime(LocalDateTime.now());
        marketCategory.setUpdateTime(LocalDateTime.now());
        MarketCategory resMarketBrand = marketCategoryService.create(marketCategory);
        // 封装对象，准备返回
        respMarketCategory(resp, resMarketBrand);

    }


    /**
     * 修改商品类别信息
     *
     * @param req
     * @param resp
     */
    @RequestMapping("/update")
    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        // 读入请求参数
        String requestBody = getRequestBody(req);
        MarketCategory marketCategory = JacksonUtil.getObjectMapper().readValue(requestBody, MarketCategory.class);
        // 检验参数正确性
        if (marketCategory.getId() == null || StringUtils.isEmpty(marketCategory.getName())) {
            resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.badArgument()));
            return;
        }
        marketCategory.setUpdateTime(LocalDateTime.now());
        // 调用service层接口
        MarketCategory respMarketCategory = marketCategoryService.update(marketCategory);
        // 封装对象，准备返回
        respMarketCategory(resp, respMarketCategory);
    }

    /**
     * 删除商品类别
     *
     * @param req
     * @param resp
     */
    @RequestMapping("/delete")
    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("delete");
        // 读入请求参数
        String requestBody = getRequestBody(req);
        MarketCategory marketCategory = JacksonUtil.getObjectMapper().readValue(requestBody, MarketCategory.class);
        // 检验参数正确性
        if (marketCategory.getId() == null || StringUtils.isEmpty(marketCategory.getName())) {
            resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.badArgument()));
            return;
        }
        // code 0:成功 1:失败
        marketCategory.setUpdateTime(LocalDateTime.now());
        int code = marketCategoryService.delete(marketCategory);
        if (code == 0) {
            resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.ok()));
            return;
        }
        // 删除失败
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.badArgumentValue()));

    }

    /**
     * 封装对象，准备返回
     *
     * @param resp
     * @param marketCategory
     * @throws IOException
     */
    private static void respMarketCategory(HttpServletResponse resp, MarketCategory marketCategory) throws IOException {
        if (marketCategory != null) {
            resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.ok(marketCategory)));
        } else {
            resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.badArgument()));
        }
    }


}
